<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');
define('USER_PASSWORD', 'USER_PASSWORD_KEY');

class UserController extends JLControllerBase {

    private $config = null;
    private $path = "User";
    private $title = "Système d'Actualisation CSME > Utilisateurs";
    private $data = null;

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function Index() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();

        require JLPATH_MODELS . DS . "TypeUsersModel.php";
        $typeusers = new TypeUsersModel();
        $data['typeusers'] = $typeusers->getTypeUsers();

        require JLPATH_MODELS . DS . "StateModel.php";
        $states = new StateModel();
        $data['states'] = $states->getStates();

        $data['texttitle'] = " > Utilisateurs";
        $data['classcurrent2'] = ' class="current"';
        $this->View->Show($this->path, $this->title, $data);
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function SaveUser() {
        require JLPATH_MODELS . DS . "UserModel.php";
        $user = new UserModel();
        $jl = new JLTools();

        $var_username = $_POST['txtusuario'];
        $var_fullname = $_POST['txtnombre'];
        $var_email = $_POST['txtemail'];
        $var_pwd = $_POST['txtclave'];
        $dat_creation = $jl->FechaActual("ambos", "anormal");
        $dat_modification = $jl->FechaActual("ambos", "anormal");
        $int_id_section = $_POST['cboseccion'];
        $int_id_typeuser = $_POST['cbotipousuario'];
        $int_id_state = $_POST['cboestado'];

        unset($_SESSION[USER_PASSWORD]);
        echo $user->SaveUser($var_fullname, $var_email, $var_username, $var_pwd, $dat_creation, $dat_modification, $int_id_section, $int_id_typeuser, $int_id_state);
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function SaveUserUpdate() {
        $_id = $_POST['_id'];
        require JLPATH_MODELS . DS . "UserModel.php";
        $user = new UserModel();
        $jl = new JLTools();

        $var_username = $_POST['txtusuario'];
        $var_fullname = $_POST['txtnombre'];
        $var_email = $_POST['txtemail'];
        $var_pwd = $_SESSION[USER_PASSWORD];
        
        if ($var_pwd != $_POST['txtclave']) {
            $var_pwd = md5($_POST['txtclave']);
        }

        $dat_modification = $jl->FechaActual("ambos", "anormal");
        $int_id_section = $_POST['cboseccion'];
        $int_id_typeuser = $_POST['cbotipousuario'];
        $int_id_state = $_POST['cboestado'];

        echo $user->SaveUserUpdate($_id, $var_fullname, $var_email, $var_username, $var_pwd, $dat_modification, $int_id_section, $int_id_typeuser, $int_id_state);
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function getUserById() {
        $_id = $_POST['_id'];
        require JLPATH_MODELS . DS . "UserModel.php";
        $_user = new UserModel();
        $user = $_user->getUserById($_id);

        $var_username = $user['var_username'];
        $var_fullname = $user['var_fullname'];
        $var_email = $user['var_email'];
        $var_pwd = $user['var_pwd'];
        $int_id_section = $user['int_id_section'];
        $int_id_typeuser = $user['int_id_typeuser'];
        $int_id_state = $user['int_id_state'];

        $_SESSION[USER_PASSWORD] = $var_pwd;
        echo $var_fullname . "_%_" . $var_email . "_%_" . $var_username . "_%_" . $var_pwd . "_%_" . $int_id_section . "_%_" . $int_id_typeuser . "_%_" . $int_id_state;
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function deleteUsers() {
        $_ids = $_POST['_ids'];
        require JLPATH_MODELS . DS . "UserModel.php";
        $_user = new UserModel();

        $ides = explode("-", $_ids);
        $i = 0;
        foreach ($ides as $v) {
            $_true = $_user->DeleteUser(trim($v));
            if (!$_true)
                break;
            $i++;
        }

        $nodelete = count($ides) - $i;

        if (!$_true) {
            echo $i . "  Donnée(s) effacer \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        }
        else
            echo $i . "  Donnée(s) effacer";
    }

}